define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'finance/cash/index',
                    add_url: 'finance/cash/add',
                    edit_url: 'finance/cash/edit',
                    del_url: 'finance/cash/del',
                    multi_url: 'finance/cash/multi',
                    table: 'cash',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'cash_id',
                sortName: 'cash_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'cash_id', title: __('Cash_id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'user.phone', title: __('User.phone')},
                        {field: 'amount', title: __('Amount'), operate: 'BETWEEN'},
                        {field: 'cash_account', title: __('Cash_account')},
                        {field: 'cash_name', title: __('Cash_name')},
                        {field: 'status', title: __('Status'), visible: false, searchList: {"0": __('Status 0'), "1": __('Status 1'), "10": __('Status 10')}},
                        {field: 'status_text', title: __('Status'), operate: false},
                        {field: 'refusal_reason', title: __('Refusal_reason')},
                        {field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'refusetime', title: __('Refusetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
//                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                        {
                            field: 'cash_id',
                            title: __('操作'),
                            operate: false,
                            width: '110px',
                            formatter: Controller.api.formatter.opt
                        },
                    ]
                ]
            });

            $(table).on("click", "[data-id].btn-refuse", function (e) {
                e.preventDefault();
                var id = $(this).data("id");
                var that = this;
                var url = $(that).data("url");
                Layer.prompt({title: __('拒绝原因'), formType: 0}, function (value, index) {
                    Backend.api.ajax({
                        url: url + "?cash_id=" + id + "&refusal_reason=" + value,
                        data: $(that).closest("form").serialize()
                    }, function () {
                        table.bootstrapTable('refresh', {});
                    });
                    Layer.close(index);
                });
            });

            $(table).on("click", "[data-id].btn-check", function (e) {
                e.preventDefault();
                var id = $(this).data("id");
                var that = this;
                var url = $(that).data("url");
                Layer.confirm(
                        __('确定该笔提现已付款，然后进行该操作？'),
                        {icon: 3, title: __('提示'), shadeClose: true},
                        function (index) {
                            $.post(url, {cash_id: id}, function (res) {
                                table.bootstrapTable('refresh', {});
                            });
                            Layer.close(index);
                        }
                );
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {//渲染的方法
                opt: function (value, row, index) {
                    var html = '';
                    if (row.status == 0) {
                        html += '<a class="text-success btn-check"  style="cursor:pointer" data-url="finance/cash/checkok" data-id="' + row.cash_id + '"><b>已付款</b></a> | ';
                        html += '<a class="text-success btn-refuse"  style="cursor:pointer" data-url="finance/cash/checkno" data-id="' + row.cash_id + '"><b>拒绝提现</b></a>';
                    } else {
                        html += '-';
                    }
                    return html;
                }
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});